#pragma once

#include <stm32f4xx_ll_i2c.h>

#include "share/byte_buffer.h"
#include "mcu.h"


/// I2C 总线驱动数据结构
typedef struct _mcu_i2c {
	/// I2C 接口控制器
	I2C_TypeDef *port;
	/// I2C 接口硬件地址
	uint8_t addr;
    /// 接收数据缓冲区
	byte_buffer_t* rxbuf;
    /// 接收数据中断处理接口
    mcu_i2c_rxdata_handler_t rxhandler; void* rxcookie;
    /// 发送完成中断处理接口
    mcu_i2c_txempty_handler_t txhandler; void* txcookie;
} mcu_i2c_t;


#ifdef __cplusplus
extern "C" {
#endif


/// @brief I2C 接口初始化
/// @param sclpin SCL 使用的 GPIO 引脚编号
/// @param speed_mode: LL_I2C_CLOCK_SPEED_STANDARD_MODE, LL_I2C_CLOCK_SPEED_FAST_MODE
std_err_t mcu_i2c_init(mcu_i2c_t* dev, mcu_gpio_num_t sclpin, mcu_gpio_num_t sdapin, uint32_t speed_mode);

#define I2CBUS_DEFINE(NAME, DEV, SCLPIN, SDAPIN, SPEED) \
	mcu_i2c_t  _I2C_##NAME = {DEV}; \
	mcu_i2c_t*  I2C_##NAME = &_I2C_##NAME; \
	std_err_t I2C_##NAME##_Init() { return mcu_i2c_init(I2C_##NAME, MCU_GPIO_NUM_##SCLPIN, MCU_GPIO_NUM_##SDAPIN, LL_I2C_CLOCK_SPEED_##SPEED##_MODE); }

#define I2CDEV_DEFINE(NAME, I2CBUS, ADDR) \
	mcu_i2c_t* NAME##_I2C  = &_I2C_##I2CBUS; \
	uint8_t    NAME##_Addr = ADDR;
	
#ifdef __cplusplus
}
#endif
